﻿using lwzlz.Application.System.Dto;
using lwzlz.Common;
using lzwlz.Web;
using System;
using System.Web.Mvc.Filters;
using System.Web.Routing;

namespace lwzlz.Web
{
    public class StudyController : BaseController
    {
        /// <summary>
        /// 在进行授权时调用
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnAuthentication(AuthenticationContext filterContext)
        {
            StudentLogin student = StudyStudentLogin.GetCurrentUser();

            if (student == null || student.Id == 0)
            {
                var url = Request.Url;
                if (url != null && !url.ToString().Contains("Login"))
                {
                    string queryString = url.Query;
                    var param = "";
                    if (queryString.StartsWith("?id="))
                    {
                        param = queryString.Substring(4, queryString.Length - 4);
                    }

                    var urlSpt = url.ToString().Split('?');
                    var newUrl = "";
                    var absolutePath = urlSpt[0];
                    if (urlSpt.Length > 1)
                    {
                        newUrl = absolutePath + "?id=" + param;
                    }
                    else
                    {
                        newUrl = url.ToString();
                    }
                    ZCookies.WriteCookies("historyUrl", 1, newUrl);
                }

                //未登录
                filterContext.Result = RedirectToRoute(new { area = "Study", controller = "Login", action = "PassWordLogin" });
            }

            base.OnAuthentication(filterContext);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="callback"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        protected override IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state)
        {
            var studentLogin = StudyStudentLogin.GetCurrentUser();
            return base.BeginExecute(requestContext, callback, state);
        }
    }
}